package cn.wolfcode.controller;

import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.MessageConst;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class SendMSGController {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    
    @RequestMapping("/sendMsg")
    public String sendMessage(Integer age,String msg){

        Map<String,Object> map=new HashMap<>();
        //用户自定义属性
        map.put("age", age);
        map.put("name", "maomao");
        //也可以设置系统属性
        //map.put(MessageConst.PROPERTY_KEYS,age);
        rocketMQTemplate.convertAndSend("02-boot-msg",msg,map);

        //rocketMQTemplate.syncSend("01-boot-msg:taga",msg);//同步
        //rocketMQTemplate.sendOneWay("01-boot-msg",msg);//一次性
        //异步
//        rocketMQTemplate.asyncSend("01-boot-msg", msg, new SendCallback() {
//            @Override
//            public void onSuccess(SendResult sendResult) {
//                System.out.println("成功");
//            }
//
//            @Override
//            public void onException(Throwable throwable) {
//                System.out.println("失败");
//            }
//        });

//        rocketMQTemplate.syncSend("01-boot-msg",
//                MessageBuilder.withPayload(msg).build(),
//                3000, 3);
        return "发送成功";
    }
}